// pages/user-center/appointment-list/appointment-list.js
const app = getApp();
const request = require('../../../utils/request.js');
const config = require('../../../utils/config');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isResult: true,
    appointmentList: [],
    pageNum: 1,
    pageSize: 10
  },

  /**
   * 获取我的收藏列表
   */
  getAppointmentList(pageNum, pageSize){
    let that = this;
    let token = wx.getStorageSync("token");
    let url = config.SERVER_URL + "activity/list/self/appointments?pageNum=" + pageNum + "&pageSize=" + pageSize;
    wx.showLoading({
      title: '加载ing',
    })
    request.requestGetApi(url, token, {}, "application/json").then(res=>{
      if(res.status === config.STATUS_OK){
        let isResult = res.data.rows.length >= pageSize;
        this.setData({
          isResult,
          appointmentList: that.data.appointmentList.concat(res.data.rows)
        });
      }
    }).catch(err=>{
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
  },
  
  /**
   * 跳转到活动详情页面
   */
  toActivityInfo(e){
    wx.navigateTo({
      url: '/pages/activity-info/activity-info?id=' + e.currentTarget.dataset.id
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    this.setData({
      appointmentList: [],
      pageNum: 1
    })
    this.getAppointmentList(this.data.pageNum, this.data.pageSize);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      appointmentList: [],
      pageNum: 1
    });
    this.getAppointmentList(this.data.pageNum, this.data.pageSize);

    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isResult){
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1
    });
    this.getAppointmentList(this.data.pageNum, this.data.pageSize);
  }
})